package com.mvp.shq.washer.ui.home;


import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.zxing.activity.CaptureActivity;
import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.MyViewPagerAdapter;
import com.mvp.shq.washer.base.BaseFragment;
import com.mvp.shq.washer.entity.Banner;
import com.mvp.shq.washer.ui.main.MainActivity;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.view.widget.WaveView;
import com.orhanobut.logger.Logger;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;


/**
 * A simple {@link Fragment} subclass.
 */
@RuntimePermissions
public class HomeFragment extends BaseFragment<HomePresenter, HomeModel> implements HomeConstract.View {

    @BindView(R.id.viewpager)
    ViewPager vp;
    @BindView(R.id.home_bg)
    AutoLinearLayout homeBg;
    @BindView(R.id.img_scan_state)
    ImageView imgScanContent;
    @BindView(R.id.img_scan)
    ImageButton imgScan;
    @BindView(R.id.wave_view)
    WaveView waveView;
    @BindView(R.id.arl_wave)
    AutoRelativeLayout arlWave;
    private List<Banner> mList;
    private MyViewPagerAdapter adapter;
    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(getActivity(), getResources().getColor(R.color.home_status_bar), 0);
        //mList.add("http://img2.imgtn.bdimg.com/it/u=2947968725,1134033039&fm=26&gp=0.jpg");
        mList = new ArrayList<>();
        presenter.banner();
        //mList.add("http://www.zhlzw.com/UploadFiles/Article_UploadFiles/201204/20120412123929822.jpg");
    }

    private double waveUp = 0;
    @OnClick({R.id.img_scan,R.id.wave_view,R.id.arl_wave})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.img_scan:
                HomeFragmentPermissionsDispatcher.toScanWithCheck(this);
                break;
            case R.id.wave_view:
                Logger.d("？？？？？？？？？？？？？？？？？？？？？？？？");
                if (waveUp < 1){
                    waveUp += 0.1;
                    waveView.change(waveUp);
                }else {
                    waveUp = 0;
                    imgScan.setVisibility(View.VISIBLE);
                    imgScanContent.setVisibility(View.VISIBLE);
                    arlWave.setVisibility(View.GONE);
                }
                break;
        }
    }

    @NeedsPermission(Manifest.permission.CAMERA)
    public void toScan() {
//        Intent intent = new Intent(context, CaptureActivity.class);
//        startActivityForResult(intent, 7);
//        imgScan.setVisibility(View.GONE);
//        imgScanContent.setVisibility(View.GONE);
//        arlWave.setVisibility(View.VISIBLE);
//        waveView.change(0);
        JumpUtil.toOrderConfirm(context);
    }
    @OnShowRationale(Manifest.permission.CAMERA)
    void showRationaleForCamera(final PermissionRequest request) {
        new AlertDialog.Builder(context)
                .setMessage("是否开启相机权限")
                .setPositiveButton("同意", (dialog, button) -> request.proceed())
                .setNegativeButton("拒绝", (dialog, button) -> request.cancel())
                .show();
        Toast.makeText(context, "showRationaleForCamera", Toast.LENGTH_SHORT).show();
    }

    @OnPermissionDenied(Manifest.permission.CAMERA)
    void showDeniedForCamera() {
        Toast.makeText(context, "拒绝相机权限将无法使用扫码功能", Toast.LENGTH_SHORT).show();
    }

    @OnNeverAskAgain(Manifest.permission.CAMERA)
    void showNeverAskForCamera() {
        Toast.makeText(context, "关闭相机权限无法使用扫码功能，请先允许相机", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        HomeFragmentPermissionsDispatcher.onRequestPermissionsResult(this,requestCode,grantResults);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == CaptureActivity.RESULT_CODE_QR_SCAN && requestCode == 7) { //RESULT_OK = -1
            Bundle bundle = data.getExtras();
            Log.d("zing", "扫码返回:" + bundle.toString());
            String scanResult = bundle.getString(CaptureActivity.INTENT_EXTRA_KEY_QR_SCAN);
            Toast.makeText(context, scanResult, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void getBannerSuccess(List<Banner> banners) {
        if (banners != null){
            mList = banners;
            adapter = new MyViewPagerAdapter(context, mList);
            vp.setAdapter(adapter);
            //vp.setPageTransformer(true,new RotationPageTransformer());
            vp.setOffscreenPageLimit(2);
        }
    }

    @Override
    public void getBannerFail(String msg) {
        Logger.d("获取banner失败:"+msg);
    }
}
